package cn.iocoder.yudao.module.shop.controller.app.giftcard;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.shop.controller.admin.giftcard.vo.ShopGiftCardBindReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.giftcard.vo.ShopGiftCardRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.giftcard.vo.ShopGiftCardSmsCodeReqVO;
import cn.iocoder.yudao.module.shop.convert.giftcard.ShopGiftCardConvert;
import cn.iocoder.yudao.module.shop.convert.giftitem.ShopGiftItemConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.giftcard.ShopGiftCardDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.giftitem.ShopGiftItemDO;
import cn.iocoder.yudao.module.shop.service.giftcard.ShopGiftCardService;
import cn.iocoder.yudao.module.shop.service.giftitem.ShopGiftItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import javax.annotation.Resource;
import javax.validation.Valid;

@Tag(name = "用户 App - 礼品卡")
@RestController
@RequestMapping("/shop/giftcard")
@RequiredArgsConstructor
@Validated
@Slf4j
public class AppShopGiftCardController {

    @Resource
    private ShopGiftCardService shopGiftCardService;

    @Resource
    private ShopGiftItemService shopGiftItemService;

    @PostMapping("/code")
    @Operation(summary = "发送验证码")
    public CommonResult<Boolean> sendSmsCode(@Valid @RequestBody ShopGiftCardSmsCodeReqVO reqVO){
        shopGiftCardService.sendSmsCode(reqVO);

        return success(true);
    }


    @PostMapping("/bind")
    @Operation(summary = "绑定礼品卡")
    public CommonResult<Boolean> bindGiftCard(@Valid @RequestBody ShopGiftCardBindReqVO reqVO){
        shopGiftCardService.bindCard(reqVO);

        return success(true);
    }

    @GetMapping("/get")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @Operation(summary = "获取礼品卡")
    public CommonResult<ShopGiftCardRespVO> getGiftCard(@RequestParam("id") String id){
        ShopGiftCardDO giftCard = shopGiftCardService.getGiftCardByCardId(id);
        if(giftCard == null){
            return success(null);
        }

        ShopGiftItemDO itemDO = shopGiftItemService.getGiftItem(giftCard.getGiftId());
        ShopGiftCardRespVO respVO = ShopGiftCardConvert.INSTANCE.convert(giftCard);
        respVO.setGiftItem(ShopGiftItemConvert.INSTANCE.convert(itemDO));

        return success(respVO);
    }
}
